package org.springframework.samples.petclinic.web;

import java.util.Date;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

@Controller
public class SessionController {

    AtomicLong localSessionSize = new AtomicLong(0);
    
    private static Logger logger = LoggerFactory.getLogger(SessionController.class);
//    @RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
//    public String fillSession() {
//        
//        return "session/FillSession";
//    }

    @RequestMapping(value = "/session/fill", method=RequestMethod.GET)
    public String fillSession(Model model, HttpSession session, SessionStatus status) {

        int sessionSize = 0;
        Enumeration<String> enumer = session.getAttributeNames();
        while(enumer.hasMoreElements()) {
            enumer.nextElement();
            sessionSize++;
        }
        
        model.addAttribute("sessionSize", sessionSize);
        model.addAttribute("localSessionSize", localSessionSize);
        model.addAttribute("fillSession", new FillSession());
        return "session/fill";
    }
    
    @RequestMapping(value = "/session/fill", method=RequestMethod.POST)
    public String fillSession(FillSession fillSession, HttpServletRequest request, HttpSession session, SessionStatus status) {
        
        if (fillSession.isCleanup()) {
            logger.info("setting session status to complete");
            status.setComplete();
            
        } else {
            logger.info("adding " + fillSession.getCount() + " attributes into session");
            for (int i = 0; i < fillSession.getCount(); i++) {
         //       String s = new Date().toString();
                session.setAttribute(""+localSessionSize.incrementAndGet(), new Long(localSessionSize.get()));
                if (fillSession.sleep > 0) {
                    try {
                        Thread.sleep(fillSession.getSleep());
                    } catch (InterruptedException e) {
                        logger.error("blad przy powiększaniu sesji ", e);
                    }
                }
            }
        }
        logger.info("fillSession will send redirect");
        return "redirect:/session/fill";
    }

    public static class FillSession {
        private int count;
        private int size = 2;
        private boolean cleanup;

        public boolean isCleanup() {
            return cleanup;
        }

        public void setCleanup(boolean cleanup) {
            this.cleanup = cleanup;
        }

        private long sleep;

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public long getSleep() {
            return sleep;
        }

        public void setSleep(long sleep) {
            this.sleep = sleep;
        }
    }
}
